package com.stu.stusystem.controller.system;

import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiResult;
import com.stu.stusystem.common.OffsetPage;
import com.stu.stusystem.service.system.RoleJurisdictionService;
import com.stu.stusystem.service.system.RoleService;
import com.stu.stusystem.service.system.dto.RoleDTO;
import com.stu.stusystem.service.system.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: cxt
 * @time: 2021/3/31
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {

    private RoleJurisdictionService roleJurisdictionService;

    private RoleService roleService;

    @ApiOperation("分页查询")
    @GetMapping("/get")
    public PageInfo<RoleVO> getRolePage(OffsetPage query, String name) {
        return this.roleService.getRolePage(query, name);
    }

    @ApiOperation("修改角色")
    @PutMapping("/update")
    public ApiResult<String> updateRole(@RequestBody RoleDTO dto) {
        this.roleService.updateRole(dto);
        return ApiResult.success();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete")
    public ApiResult<String> deleteRole(@RequestParam("id") String id) {
        this.roleService.deleteRole(id);
        return ApiResult.success();
    }

    @ApiOperation("新增角色")
    @PostMapping("/add")
    public ApiResult<String> saveRole(@RequestBody RoleDTO dto) {
        this.roleService.saveRole(dto);
        return ApiResult.success();
    }

    @ApiOperation("根据id查找权限")
    @GetMapping("/getRole")
    public List<Object> getRoleJurisdiction(@RequestParam("id") String id) {
        return this.roleJurisdictionService.getRoleJurisdiction(id);
    }

    @ApiOperation("授权操作")
    @PostMapping("/authorize")
    public ApiResult<String> authorize(@RequestParam("id") String id, @RequestParam("menuId") String[] menuId) {
        this.roleJurisdictionService.authorization(id, menuId);
        return ApiResult.success();
    }

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Autowired
    public void setRoleJurisdictionService(RoleJurisdictionService roleJurisdictionService) {
        this.roleJurisdictionService = roleJurisdictionService;
    }
}
